import create from 'dd-store'
import pageStore from '../../stores/globalStore'
import { getDepteStaffApi } from '../../api/manage'

create.Page({
  store: pageStore,
  data: {
    // 部门ID
    deptDDId: '',
    // 部门名称
    deptName: '',
    // 员工列表
    staffList: []
  },
  onLoad(options) {
    this.setData({
      deptId: options.deptDDId,
      deptName: options.deptName
    })
    this.getDeptStaffListFunc()
  },
  // 部门员工点击事件
  staffItemClick(e) {
    let eItem = e.target.dataset.info
    console.log(eItem);
    // 判断员工是否离职
    if (eItem.status == 1) {
      dd.navigateTo({
        url: `/pages/manage/staffPerformance/staffPerformance?userDDId=${eItem.userId}&userName=${eItem.userName}`
      })
    } else {
      dd.confirm({
        title: '提示',
        content: '此员工已离职，确定查看吗？',
        cancelButtonText: '我再想想',
        confirmButtonText: '查看',
        success: (res) => {
          const { confirm } = res;
          console.log(confirm);
          if (confirm) {
            dd.navigateTo({
              url: `/pages/manage/staffPerformance/staffPerformance?userDDId=${eItem.userId}`
            })
          }
        },
        fail: () => { },
        complete: () => { },
      });
    }

  },
  // 查询部门员工的函数
  getDeptStaffListFunc() {
    dd.getStorage({
      key: 'currentCity',
      success: (res) => {
        dd.showLoading({
          content: "加载中"
        })
        let data = {
          page: 1,
          size: 50,
          userDDId: res.data.value.userId,
          deptDDId: this.data.deptDDId,
          userName: ''
        }
        getDepteStaffApi(data).then((res) => {
          console.log('部门员工', res);
          dd.hideLoading()
          this.setData({
            staffList: res.data.list
          })
        }).catch(() => {
          dd.hideLoading()
        })

      },
      fail: function (res) {
        dd.alert({ content: res.errorMessage });
      }
    });
  }
});
